package com.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dao.UserDAO;
import com.pojo.User;
//import com.token.JwtUtil;
import com.util.MD5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;


/**
 * User: Qsxmj
 * Date: 2019/10/9 16:02
 * Content:
 *
 * @author qsxmji
 */
@SuppressWarnings("all")
@Service
public class UserService extends ServiceImpl<UserDAO,User> {

    @Autowired
    private UserDAO userDAO;
    @Autowired
    private RedisTemplate redisTemplate;

    public String checkPassword(String username){
        return userDAO.chechPassword(username);
    }

    public int  updatePasswordByName(String username,String newPassword){
        return userDAO.updatePasswordByName(username,newPassword);}

    /**
     * @param id       用户id
     * @param password 要修改的密码
     * @return
     */
    public int updatePassword(Integer id, String password) {
        return userDAO.updatePasswordById(id, password);
    }

    /**
     * @param id          用户id
     * @param oldPassword 密码
     * @return 验证数据库是是否有该用户时
     */
    public int checkPassword(Integer id, String oldPassword) {
        int n = userDAO.checkPassword(id, oldPassword);
        return n;
    }

    /**
     *
     * @param username 接收界面上的用户名
     * @return  查询数据库已存在的用户名
     */
    public User findUser(String username){
        return userDAO.findUser(username);
    }

    /**
     *  用户登录
     * @param username  页面传过来的账号
     * @param password  页面传过来的密码
     * @return
     */
    public User login(String username, String password) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("user_name",username);
        queryWrapper.eq("user_password", MD5Util.string2MD5(password));
        List<User> users = userDAO.selectList(queryWrapper);
        if(users.size()==1){
            return users.get(0);
        }
        return null;
    }

    public int updateRole(String username, int roleId) {
        return userDAO.updateRole(username,roleId);
    }
}
